/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package course.ya.doc.from.code.model.statements;

import java.util.regex.Pattern;

public class StatementFactory implements IStatementFactory {
    private static StatementFactory instance;

    private StatementFactory() {
    };

    public static IStatementFactory getInstance() {
        if (instance == null) {
            instance = new StatementFactory();
        }
        return instance;
    }

    @Override
    public AStatement getStatement(String stmt) {
        AStatement statement;
        String st = stmt.trim();
       
        if (Pattern.compile("for\\s+[(]{1}.*[)]{1}\\{?").matcher(st).matches()) {
            statement = new ForStatement(st);
        }
        else if (Pattern.compile("while\\s+[(]{1}.*[)]{1}\\{?").matcher(st).matches()) {
            statement = new WhileStatement(st);
        }
        else if (Pattern.compile("switch\\s+[(]{1}.*[)]{1}\\{?").matcher(st).matches()) {
            statement = new SwitchStatement(st);
        }
        else if (Pattern.compile("if\\s+[(]{1}.*[)]{1}\\{?").matcher(st).matches()) {
            statement = new IfStatement(st);
        }
        else if (Pattern.compile("else\\s+if\\s+[(]{1}.*[)]{1}\\{?").matcher(st).matches() || Pattern.compile("case\\s+.*:").matcher(st).matches()) {
            statement = new ElsIfStatement(st);
        }
        else if (Pattern.compile("else\\s+.+").matcher(st).matches()) {
            statement = new ElseStatement();
        }
        /*
        else if (Pattern.compile("^([_A-z]{1}\\S*){1}\\s*=\\s*(.)+").matcher(st).matches()) {
            int index = st.indexOf("=");
            
            statement=new Assignment(st.substring(0, index).trim(), new Expression(st.substring(index + 1, st.length()).trim()));
        }
        
        else if (Pattern.compile("[_A-z]{1}[A-z0-9_\\[\\]]+\\s+[A-z0-9]+.*").matcher(st).matches()) {
            String expr = st.substring(st.indexOf(" "), st.indexOf(";"));
            String[] vars = expr.split(",");
            String variables = "";
            for (int i = 0; i < vars.length; i++) {
                String[] spl = vars[i].split("=");
                variables += spl[0];
            }
            statement = new PrimitiveDeclaration(st.substring(0, st.indexOf(" ")), variables, new Expression(expr));
        }
        */
        else {
            statement = null;
        }

        return statement;
    }
 
}
